/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jbfilter.jsp.fcomps;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;

import org.jbfilter.core.EnableStrategy;
import org.jbfilter.core.FilterComponentContainer;
import org.jbfilter.jsp.FilterComponentContainerTag;
import org.jbfilter.jsp.FilterTagCallable;

public abstract class LogicFilterComponentTag<E>
	extends
		FilterComponentTag<E>
	implements
		FilterComponentContainerTag<E> {
	
	@Override
	public void doTag() throws JspException, IOException {
		super.doTag();
		// invoke the body
		JspFragment body = getJspBody();
		if (body != null) {
			body.invoke(null);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public FilterComponentContainer<E> getFilterComponentContainer() {
		return (FilterComponentContainer<E>)getFilterComponent();
	}

	@Override
	public void registerFilterTagCallable(FilterTagCallable callable)
			throws JspException {
		// bubble registering up to the filter tag
		FilterComponentContainerTag<E> parent = retrieveParent();
		parent.registerFilterTagCallable(callable);
	}
	// TODO verify if this is correct behavior
	@Override
	protected EnableStrategy getJspEnableStrategy() {
		return new EnableStrategy() {
			public boolean decideEnabled() {
				return getFilterComponent().isEnabled();
			}
		};
	}
	@Override
	public String getProviderName() {
		return super.getProviderName();
	}
	@Override
	public String getProviderClassName() {
		return super.getProviderClassName();
	}
}
